#include"Link.h"

Link* Link::insert(Link* n)
{
  if(this==nullptr) return n;
  if(n==nullptr) return this;
  n->succ=this;
  n->pre=this->pre;
  if(this->pre)
    this->pre->succ=n;
  this->pre=n;
  return n;
}

Link* Link::add(Link* n)
{
  if(this==nullptr) return n;
  if(n==nullptr) return this;
  n->pre=this;
  n->succ=this->succ;
  if(this->succ)
    this->succ->pre=n;
  this->succ=n;
  return n;
}

Link* Link::erase()
{
  if(this==nullptr) return nullptr;
  if(this->pre)
    this->pre->succ=this->succ;
  if(this->succ)
    this->succ->pre=this->pre;
  return this->succ;
}

Link* Link::find(const string& s)
{
  Link* p=this;
  while(p)
    {
      if(p->val==s) return p;
      p=p->succ;
    }
  return nullptr;
}

Link* Link::move(int n)   //n being positive means moving backwards,otherwise,moving forwards
{
  if(this==nullptr) return nullptr;
  Link* p=this;
  if(n>0)
    {
      while(n--)
	{
	  if(p==nullptr) return nullptr;
	  p=p->succ;
	}
    }
  else if(n<0)
    {
      while(-(n++))
	{
	  if(p==nullptr) return nullptr;
	  p=p->pre;
	}
    }
  return p;
}
